package com.team2.insulatingtools.modular.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.basic.SimpleRoleInfo;
import cn.stylefeng.roses.kernel.rule.tree.factory.DefaultTreeBuildFactory;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.team2.insulatingtools.modular.system.entity.SimpleOrgTreeNode;
import com.team2.insulatingtools.modular.system.entity.ToolsHrOrganization;
import com.team2.insulatingtools.modular.system.mapper.ToolsHrOrganizationMapper;
import com.team2.insulatingtools.modular.system.pojo.request.ToolsHrOrganizationRequest;
import com.team2.insulatingtools.modular.system.service.OrgService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author yangqian
 * @date 2023-04-05 16:33
 * @description
 **/
@Service
public class OrgServiceImpl implements OrgService {

    @Resource
    ToolsHrOrganizationMapper mapper;

    /**
     * 获取当前用户所在组织机构及下属机构信息
     */
    @Override
    public List<ToolsHrOrganization> getSubOrgByCurrent() {
        LoginUser user = LoginContext.me().getLoginUser();
        return getSubOrg(user.getOrganizationId());
    }

    @Override
    public List<ToolsHrOrganization> getSubOrg(Long orgId){
        return mapper.selectSubOrgs(orgId);
    }

    /**
     * 根据当前角色获取组织机构
     * @param speciality 专业
     * @return
     */
    @Override
    public List<ToolsHrOrganization> getOrgsByRole(String speciality) {
        LoginUser user = LoginContext.me().getLoginUser();
        List<SimpleRoleInfo> roles = user.getSimpleRoleInfoList();
        Long orgId = null;
        for (SimpleRoleInfo role :roles) {
            if(role.getRoleCode().indexOf("workshop/team") > -1){
                orgId = user.getOrganizationId();
            }
        }
        return mapper.selectOrgsByRole(orgId,speciality);
    }

    @Override
    public List<SimpleOrgTreeNode> getTreeBySpeciality(ToolsHrOrganizationRequest request) {
        LambdaQueryWrapper<ToolsHrOrganization> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(ObjectUtil.isNotNull(request.getSpeciality()),ToolsHrOrganization::getSpeciality,
                request.getSpeciality());
        List<ToolsHrOrganization> hrOrganizationList = mapper.selectList(wrapper);
        List<SimpleOrgTreeNode> simpleNodeTree = CollectionUtil.newArrayList();
        // 组装节点
        for (ToolsHrOrganization hrOrganization : hrOrganizationList) {
            SimpleOrgTreeNode treeNode = parseOrganizationTreeNode(hrOrganization);
            simpleNodeTree.add(treeNode);
        }
        List<SimpleOrgTreeNode> tree = new DefaultTreeBuildFactory<SimpleOrgTreeNode>().doTreeBuild(simpleNodeTree);
        return tree;
    }


    public static SimpleOrgTreeNode parseOrganizationTreeNode(ToolsHrOrganization hrOrganization) {
        SimpleOrgTreeNode treeNode = new SimpleOrgTreeNode();
        treeNode.setValue(hrOrganization.getOrgId());
        treeNode.setTitle(hrOrganization.getOrgName());
        treeNode.setId(hrOrganization.getOrgId());
        treeNode.setParentId(hrOrganization.getOrgParentId());
        return treeNode;
    }

}
